<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px ;">
          <el-col :span="20">
            <svg-icon icon-class="news" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree
          :data="list"
        >

          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ;width: 100%">
              <el-col :span="20">
                <svg-icon icon-class="news" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="belAdd(data.id)">删除部门</el-dropdown-item>

                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>

        </el-tree>
      </el-card>
      <!-- 添加||修改输入框 -->
      <el-dialog
        :title="isEdit?'编辑部门':'添加部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="dialogVisible"
        width="50%"
      >
        <!-- <span>这是一段信息</span> -->
        <!-- 使用 v-if 解决数据不更新的问题 -->
        <deptDialog
          v-if="dialogVisible"
          :id="isId"
          :is-edit="isEdit"
          :origin-list="originList"
          @colde="dialogVisible = false"
          @success="Hsuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getData, deleteData } from '@/api/departments'
import { pinbu } from '@/utils/pinpu'
import deptDialog from './deptDialog'
export default {
  name: 'VueWhRzDepartments',

  components: {
    deptDialog
  },

  data() {
    return {
      list: [],
      dialogVisible: false,
      isId: '',
      isEdit: false, // false新增 || true编辑
      originList: []
    }
  },

  created() {
    this.loggetData()
  },

  methods: {
    async loggetData() {
      try {
        const res = await getData()
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        //  处理数据，在传给子组件
        this.originList = res.data.depts.map(item => {
          const { id, pid, name, code } = item
          return { id, pid, name, code }
        })

        res.data.depts.shift()
        this.list = pinbu(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },

    Hsuccess() {
      this.dialogVisible = false
      this.loggetData()
    },
    // 新增
    hAdd(id) {
      // console.log(id)
      this.dialogVisible = true
      this.isId = id
      this.isEdit = false
    },
    // 编辑
    hEdit(id) {
      this.dialogVisible = true
      this.isId = id
      this.isEdit = true
    },
    // 删除
    async belAdd(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await deleteData(id)
          // console.log(res, '删除')
          if (res.code === 10000) {
            this.$message.success(res.message)
            this.loggetData()
          }
        } catch (error) {
          // console.log(error)
          this.$message.error(error)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
